﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Linq.Expressions;
using Small.Html.Entities;
using Small.Html.Common;
using Small.Html.Options;

namespace Small.Html.UI
{
   public static class HtmlNumberBox
    {

       /// <summary>
        /// 数字框
       /// </summary>
       /// <typeparam name="TModel">模型</typeparam>
       /// <typeparam name="TProperty"></typeparam>
       /// <param name="helper"></param>
       /// <param name="expression">Func<T>表达式</param>
       /// <param name="id">ID</param>
       /// <param name="numberFormat">格式</param>
       /// <param name="htmlAttribute">Html属性</param>
       /// <returns></returns>
       public static MvcHtmlString NumberBoxFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression,string id,NumberBoxOptions numberFormat,object htmlAttributes=null)
       {


         
          //获取实体类中值
           HtmlModelItem htmlItem = HtmlModelHelper.GetProperties(helper, expression);
           return new  MvcHtmlString(GenerateHtml(htmlItem, id, numberFormat, htmlAttributes));


           
          
       }

       /// <summary>
       /// 数字框
       /// </summary>
       /// <typeparam name="TModel">模型</typeparam>
       /// <typeparam name="TProperty">Func<T></typeparam>
       /// <param name="helper"></param>
       /// <param name="expression">Func<T>表达式</param>
       /// <param name="numberFormat">格式</param>
       /// <param name="id">名称</param>
       /// <param name="htmlAttributes">Html属性</param>
       /// <returns></returns>
       public static MvcHtmlString NumberBoxFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, NumberBoxOptions numberFormat,string id=null, object htmlAttributes = null)
       {


           return NumberBoxFor(helper, expression, id, numberFormat, htmlAttributes);


       }

       /// <summary>
       /// 数字框
       /// </summary>
       /// <typeparam name="TModel"></typeparam>
       /// <typeparam name="TProperty"></typeparam>
       /// <param name="helper"></param>
       /// <param name="expression"></param>
       /// <param name="numberFormat"></param>
       /// <param name="htmlAttributes"></param>
       /// <returns></returns>
       public static MvcHtmlString NumberBoxFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, NumberBoxOptions numberFormat, object htmlAttributes = null)
       {


           return NumberBoxFor(helper, expression,null, numberFormat, htmlAttributes);


       }

       /// <summary>
       /// 数字框
       /// </summary>
       /// <param name="helper"></param>
       /// <param name="name">标签名称</param>
       /// <param name="id">ID</param>
       /// <param name="value">值</param>
       /// <param name="numberFormat">格式</param>
       /// <param name="htmlAttributes">Html属性</param>
       /// <returns></returns>
       public static MvcHtmlString NumberBox(this HtmlHelper helper, string name, string id,string value, NumberBoxOptions numberFormat, object htmlAttributes=null) 
       {
           StringBuilder builder = new StringBuilder();
           //获取实体类中值
           HtmlModelItem htmlItem = new HtmlModelItem { Name = name, Value = value };

           return new MvcHtmlString(GenerateHtml(htmlItem, id, numberFormat, htmlAttributes)); 
         
       }

       /// <summary>
       /// 数字框
       /// </summary>
       /// <param name="helper"></param>
       /// <param name="name">名称</param>
       /// <param name="id">ID</param>
       /// <param name="numberFormat">格式</param>
       /// <param name="htmlAttributes">Html属性</param>
       /// <returns></returns>
       public static MvcHtmlString NumberBox(this HtmlHelper helper, string name, string id, NumberBoxOptions numberFormat, object htmlAttributes = null)
       {

           return NumberBox(helper, name, id, "0", numberFormat, htmlAttributes);

       }


       /// <summary>
       /// 数字框
       /// </summary>
       /// <param name="helper"></param>
       /// <param name="name">名称</param>
       /// <param name="numberFormat">格式</param>
       /// <param name="value">值</param>
       /// <param name="htmlAttributes">Html属性</param>
       /// <returns></returns>
       public static MvcHtmlString NumberBox(this HtmlHelper helper, string name, NumberBoxOptions numberFormat,string value="0", object htmlAttributes = null)
       {

           return NumberBox(helper, name, null, value, numberFormat, htmlAttributes);

       }
       /// <summary>
       /// 生成HTML
       /// </summary>
       /// <returns></returns>
       private static string GenerateHtml(HtmlModelItem htmlItem, string id,NumberBoxOptions numberFormat, object htmlAttributes=null) 
       {

           StringBuilder builder = new StringBuilder();

           builder.Append(String.Format("<input type=\"text\" name=\"{0}\" class=\"{1}\"  id=\"{2}\" value=\"{3}\"   {4}='true'",
               htmlItem.Name, numberFormat.Class,
               String.IsNullOrEmpty(id)?htmlItem.Name:id,
                htmlItem.Value,
                numberFormat.ValidFlag));
           builder.Append(" " + DataOption(numberFormat));
           builder.Append(" "+numberFormat.GetMessage());
           builder.Append(BaseHelper.FormatGenerateAttribute(htmlAttributes));
           builder.Append(" />\r\n");
           return builder.ToString();
       
       }


       /// <summary>
       /// 配置data-optons节点
       /// </summary>
       /// <param name="numberFormat"></param>
       /// <returns></returns>
       public static string DataOption(NumberBoxOptions numberFormat) 
       {
           StringBuilder builder = new StringBuilder();
           builder.Append("data-options=\"");

           //最小值
           builder.Append("min:" + numberFormat.Min+",");
           //最大值
           builder.Append("max:" + numberFormat.Max+",");
           //小数点
           builder.Append("precision:" + numberFormat.Precision+",");
           //前缀
           if (!String.IsNullOrEmpty(numberFormat.Prefix)) 
           {
               builder.Append(",prefix:'" + numberFormat.Prefix + "'");
           }
           //分隔符
           if (string.IsNullOrEmpty(numberFormat.GroupSeparator)) 
           {

               builder.Append("groupSeparator:'" + numberFormat.GroupSeparator + "',");
           }
           
           //小数点分隔读
           if(String.IsNullOrEmpty(numberFormat.DecimalSeparator))
           {
            numberFormat.DecimalSeparator=".";
           }
           builder.Append("decimalSeparator:'"+numberFormat.DecimalSeparator+"'");

           //是否必填
           if (numberFormat.Required)
           {
               if (numberFormat.Required) 
               {
                   builder.Append("," + numberFormat.GetRequired());
               }
           }
           //其他节点
           if (!String.IsNullOrEmpty(numberFormat.OtherDataOptions)) 
           {
               builder.Append(" " + numberFormat.OtherDataOptions);
           }
           //是否可见
           if (numberFormat.Disabled) 
           {
               builder.Append(","+numberFormat.GetDisabled());
           }


           builder.Append("\"");

           return BaseHelper.SubSymbal(builder);
       
       }

      
       
    }
}
